
using System.Collections.Specialized;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;

namespace SendMailEX
{
    /// <summary>
    /// .NET CORE 发送邮件扩展
    /// </summary>
    public static class SendMailExQQ
    {
        /// <summary>
        /// 发送邮件的邮箱地址
        /// </summary>
        /// <value></value>
        public static string MailUserName { get; set; } = "xxxxxxxx@qq.com";

        /// <summary>
        /// 邮箱密码或授权码：QQ或、163之类的邮箱需要用授权码
        /// </summary>
        /// <value></value>
        public static string MailPassword { get; set; } = "xxxxxxxxxxxxxxxx";

        /// <summary>
        /// 邮箱地址的显示名称
        /// </summary>
        /// <value></value>
        public static string MailName { get; set; } = "邮箱显示名";

        /// <summary>
        /// 发件服务器对应的smtp服务器地址
        /// </summary>
        /// <value></value>
        public static string MailServer { get; set; } = "pop.qq.com";

        /// <summary>
        /// 设置服务器是否启用SSL，默认True
        /// </summary>
        /// <value></value>

        public static bool enableSsl{ get; set; } =  true;

        /// <summary>
        /// 设置SMTP的端口，默认是腾讯的SSL-- 587
        /// </summary>
        /// <value></value>

        public static int smtpPort { get; set; } = 587;

        /// <summary>
        /// 邮件正文编码，默认 UTF-8
        /// </summary>
        /// <value></value>
        public static string emailEncoding { get; set; } = "UTF-8";

        /// <summary>
        /// 设置邮件正文是否是Html，默认true
        /// </summary>
        /// <value></value>
        public static bool emailIsBodyHtml { get; set; } = true;

        /// <summary>
        ///    .NET CORE 同步发送邮件，请不要群发邮件（否则您的邮箱很可能会被国际反垃圾邮件组织拉黑）
        /// </summary>
        /// <param name="emailSubject">邮件主题</param>
        /// <param name="emailBody">邮件正文内容</param>
        /// <param name="emailToAddress">收件人邮箱地址Aaary[]</param>
        /// <param name="emailCCAddress">抄送人邮箱地址Aaary[]</param>
        /// <param name="emailBccAddress">密抄人邮箱地址Aaary[]</param>
        /// <returns>邮件发送是否成功</returns>
        public  static bool Send(
            string emailSubject,
            string emailBody,
            string[] emailToAddress,
            string[]? emailCCAddress,
            params string[]? emailBccAddress)
        {
            try
            {
                MailMessage  mailMessage = new MailMessage();

                //收件人
                if ( !emailToAddress.Any(x => string.IsNullOrWhiteSpace(x)))
                {
                    //指定邮件收件人邮箱地址
                    foreach(string strAddress in emailToAddress)
                    {
                        mailMessage.To.Add(new MailAddress(strAddress));
                    }
                }

                //抄送收件人
                if ( !emailCCAddress.Any(x => string.IsNullOrWhiteSpace(x)))
                {
                    //指定邮件收件人邮箱地址
                    foreach(string strAddress in emailCCAddress)
                    {
                        mailMessage.CC.Add(new MailAddress(strAddress));
                    }
                }

                //密抄送收件人
                if ( !emailBccAddress.Any(x => string.IsNullOrWhiteSpace(x)))
                {
                    //指定邮件收件人邮箱地址
                    foreach(string strAddress in emailBccAddress)
                    {
                        mailMessage.Bcc.Add(new MailAddress(strAddress));
                    }
                }

                //指定发件人邮箱地址以及显示的名称
                mailMessage.From = new MailAddress(MailUserName, MailName);

                //指定抄送人邮箱地址
                //mailMessage.CC.Add(new MailAddress(emailCCAddress));


                //设置邮件正文编码
                mailMessage.BodyEncoding = Encoding.GetEncoding(emailEncoding);
                mailMessage.Body = emailBody;

                //设置邮件主题编码
                mailMessage.SubjectEncoding = Encoding.GetEncoding(emailEncoding);
                //指定邮件主题
                mailMessage.Subject = emailSubject;
                //指定邮件正文是否是HTML格式
                mailMessage.IsBodyHtml = emailIsBodyHtml;

                //创建SmtpClient实例(并指定SMTP发件服务器地址、和端口)
                SmtpClient smtpclient = new SmtpClient(MailServer, smtpPort);

                smtpclient.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword);
                //smtpclient.Port = smtpSslPort;
                //SSL连接
                smtpclient.EnableSsl = enableSsl;
                //SmtpClient 发送邮件
                smtpclient.Send(mailMessage);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

    }

    public class Async
    {
    }
}